package com.jerry.template.module.mianpage.shouye.recommend.home.fragment;

import android.os.Bundle;
import android.support.annotation.NonNull;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jerry.template.R;
import com.jerry.template.base.BaseFragment;
import com.jerry.template.constant.Urls;
import com.jerry.template.module.mianpage.shouye.recommend.home.adapter.MyUserDetailAdapter;
import com.jerry.template.module.mianpage.shouye.recommend.home.entity.TouGaoEntity;
import com.jerry.template.utils.EventMessage;
import com.jerry.template.utils.OkHttpUtils;

import java.util.ArrayList;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;

/**
 * Created by Administrator on 16-5-13.
 */
public class UserViewPagerFragment extends BaseFragment {
    //用户id
    private String user_id;
    //下拉列表为了显示用户数据
    @BindView(R.id.user_down_refreshListView)
    public PullToRefreshListView ptrListView;
    //适配器
    private MyUserDetailAdapter adapter;
    //数据解析后需要载入的实体类
    private List<TouGaoEntity.DataBean.DataBean1> tougaoList;

    @Override
    protected void getReqParams(Bundle bundle) {
        user_id = bundle.getString("user_id");
    }

    // 务必实现空参数构造方法
    public UserViewPagerFragment(){

    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_user_tougao_shoucang_pinglun;
    }
    private  int count =10;
    @Override
    protected void initViews() {
        //下拉刷新设置
        ptrListView.getRefreshableView().setDivider(null);
        ptrListView.getRefreshableView().setSelector(android.R.color.transparent);
        //设置刷新模式
        ptrListView.setMode(PullToRefreshBase.Mode.BOTH);
        //设置监听器
        ptrListView.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            //下拉刷新的监听
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                //加载第一页数据,清理之前已经加在过的数据
                loadData();
            }
            //上拉刷新的监听
            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                //加载下一页数据
                count++;
                loadData();
            }
        });
        tougaoList = new ArrayList<>();
        adapter = new MyUserDetailAdapter(getContext(),tougaoList);
        ptrListView.setAdapter(adapter);
    }

    @Override
    protected void loadData() {
//        Map<String,Object> params = new HashMap<>();
//        params.put("count",count+"");
        //访问用户投稿的接口
        OkHttpUtils.postSubmitForm(Urls.USER_DETAIL_TOUGAO_BEFORE
                +user_id
                +Urls.USER_DETAIL_TOUGAO_AFTER_COUNTBEFORE
                +count
                +Urls.USER_DETAIL_TOUGAO_AFTER_COUNTAFTER,null);
        //访问用户搜藏的内容

        //访问用户评论的内容

    }

    @Override
    protected void postEventResult(@NonNull EventMessage message) {
        if(message!=null){
            if(message.type.equals(Urls.USER_DETAIL_TOUGAO_BEFORE
                    +user_id
                    +Urls.USER_DETAIL_TOUGAO_AFTER_COUNTBEFORE
                    +count
                    +Urls.USER_DETAIL_TOUGAO_AFTER_COUNTAFTER)){
//                Log.e("UserViewPagerFragment：",message.result);
                ptrListView.onRefreshComplete();
                tougaoList.clear();
                Gson gson = new Gson();
                TouGaoEntity touGaoEntity = gson.fromJson(message.result, TouGaoEntity.class);
                if(touGaoEntity!=null){
                    tougaoList.addAll(touGaoEntity.getData().getData());
                    adapter.notifyDataSetChanged();
                }
            }
        }
    }
}
